<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <title>研线文库-你的移动考研图书馆！</title>
    <link rel="stylesheet" href="/public/images/library/css/wklogin.css">
    <script src="/public/images/library/js/adapt.js"></script>
</head>
<body>
    <div class="login_box1 ">
        <img class="back" src="/public/images/library/images/leftB.png" alt="">
        <div class="bg">
            <img src="/public/images/library/images/wk_bg.png" alt="">
        </div>
        <input type="hidden" value="{$reurl}" id="returnUrl">
        <div class="login_box">
            <div class="title">登录研线文库</div>
            <div class="longin_wrap">
                <div class="hader_item active">
                    普通登录
                </div>
                <div class="hader_item">
                    手机快捷登录
                </div>
            </div>
            <div class="connent_wrap">
                <div class="connent_item">
                    <div class="input_box">
                        <img src="/public/images/library/images/code.png" alt="">
                        <input class="phone" type="text" name="" id="username" placeholder="手机号">
                    </div>
                    <div class="input_box">
                        <img src="/public/images/library/images/sj.png" alt="">
                        <input class="m_code" type="password" name="" id="pwd" placeholder="密码">
                    </div>
                    <div class="input_box">
                        <button id="login" value="登录">登录</button>
                        <div class="bot">
                            <a href="/users/wkreg.ejs">注册</a>
                            <a class="gray" href="javascript:;">忘记密码？</a>
                        </div>
                    </div>
                </div>
            </div>

            <div class="connent_wrap2">
                <div class="connent_item">
                    <div class="input_box zj">
                        <input type="hidden" value="Quick" id="quick">
                        <img src="/public/images/library/images/code.png" alt="">
                        <input class="phone" type="text" name="" id="phone" placeholder="手机号">
                    </div>
                    <div class="input_box zj">
                        <img src="/public/images/library/images/code.png" alt="">
                        <input class="phone" type="text" name="" id="yztxm" placeholder="图形码">
                        <img id="txm" src="/login/index/verify" alt="captcha"  class="verify_div" />
                    </div>
                    <div class="input_box zj">
                        <img src="/public/images/library/images/code.png" alt="">
                        <input class="phone" type="text" name="" id="phonecodeDiv" placeholder="动态码">
                        <div id="dtm">
                            获取
                        </div>
                    </div>
                    <div class="input_box zj input_box1">
                        <input type="submit" id="quicklogin" value="登录">
                        <div class="bot">
                            <span id="reg">注册</span>
                            <span>忘记密码？xxxx</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="/public/images/library/js/jquery.js"></script>
    <script src="/public/images/library/js/md5.js"></script>
    <script src="/public/images/library/js/layer/mobile/layer.js"></script>
    <script>
        var timerid = null;
        $('.hader_item').click(function () {
            $(this).addClass('active').siblings().removeClass('active');
            var idx = $(this).index();
            if (!idx) {
                $('.connent_wrap2').fadeOut()
                console.log(timerid, "o");
                if (timerid) { return }
                timerid = setTimeout(() => {
                    $('.connent_wrap').fadeIn()
                    $('.bot').show()
                    $('.title').html("登录研线文库")
                    timerid = null
                }, 350)
            } else {
                $('.connent_wrap').fadeOut()
                if (timerid) { return }
                timerid = setTimeout(() => {
                    $('.connent_wrap2').fadeIn()
                    $('.bot').hide()
                    $('.connent_wrap2 #login').css({
                        "border-radius": '0'
                    })
                    timerid = null
                }, 350)
            }
        })

        var errorTip = $("#login_error_tip");//错误提示
        var userInput = $("#username");//用户名dom
        var pwdInput = $("#pwd");//密码dom

        /*手机验证*/
        function IsMoble(num) {
            var phone = num || 0;
            phone = $.trim(phone);
            var Format = /^1[3,4,5,6,7,8,9]\d{9}$/;
            var Is = Format.test(phone) ? true : false;
            return Is;
        }
        /*登录验证非空*/
        function LoginisNotNull(username, pwd) {
            var flag = true;
            if ($.trim(username) == "" || $.trim(pwd) == "") {
                layer.open({
                    content: '请填写用户名或密码',
                    skin: 'msg',
                    time: 2,
                })
                flag = false;
            }
            return flag;
        }
        $("#login").click(function () {
            var username = userInput.val();
            var pwd = pwdInput.val();
            var loginSubmit = $("#login");
            var flag = true;
            if (LoginisNotNull(username, pwd)) {
                if (!IsMoble(username)) {
                    layer.open({
                        content: '请填写一个有效的手机号码',
                        skin: 'msg',
                        time: 2,
                    })
                    flag = false;
                }
                if (flag) {
                    $.ajax({
                        url: "users/login/login",
                        type: "get",
                        data: ({
                            tel: $("#username").val(),
                            pwd: $("#pwd").val()
                        }),
                        dataType: "json",
                        success: function (data) {
                            console.log(data);
                            
                            if (data.code == 1 || data.code==200 ) {
                                $(".input_box #login").html("登录中...");
                                console.log(data.data.token,'data.data.token');
                                setTimeout(function () {
                                    // console.log($('#returnUrl').val());
                                    location.href ='users/my.ejs';
                                    localStorage.setItem('token',data.data.token)
                                }, 1500);
                            } else {
                                layer.open({
                                    content: data.msg,
                                    skin: 'msg',
                                    time: 2,
                                })

                            }
                        }
                    });
                }
            }
        })

        var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        function phone_code(obj, targetData) {
            var timestamp = new Date().getTime();
            var phone = $("#phone").val();
            var sign = "username=" + phone + "&time" + timestamp;
            var sign5 = hex_md5(sign);
            var captcha = $("#yztxm").val();
            //验证移动电话
            var data = targetData;
            console.log(data, '===');
            if (!(myreg.test(data))) {
                // $("#errorInfo1").html("请输入正确的手机号！");
                // $("#checkPhone1").show();
                layer.open({
                    content: '请输入正确的手机号！',
                    skin: 'msg',
                    time: 2,
                })
                return false;
            }
            console.log(captcha, "--captcha");
            if (captcha == "") {
                // $("#errorInfo1").html("请输入图形验证码!");
                // $("#checkPhone1").show();
                layer.open({
                    content: '请输入图形验证码！',
                    skin: 'msg',
                    time: 2,
                })
                return false;
            }
            //验证登录名称是否重复
            $.ajax({
                // url: "/login/index/code",
                type: "post",
                dataType: "json",
                data: {
                    "username": data,
                    "sign": sign5,
                    "time": timestamp,
                    "captcha": captcha,
                    "source": 2
                },
                success: function (data) {
                    // console.log(data,"--cg");
                    if (data.success == 1) {
                        invokeSettime(obj);
                    } else {
                        layer.open({
                            content: data.msg,
                            skin: 'msg',
                            time: 2,
                        })
                    }
                }
            });
        }

        function invokeSettime(obj) {
            var countdown = 60;
            settime(obj);

            function settime(obj) {
                if (countdown == 0) {
                    $(obj).attr("disabled", false);
                    $(obj).html("获取验证码");
                    countdown = 60;
                    return;
                } else {
                    $(obj).attr("disabled", true);
                    $(obj).html("(" + countdown + ") s 获取");
                    countdown--;
                }
                setTimeout(function () {
                    settime(obj)
                }, 1000)
            }
        }


        $("#dtm").click(function () {
            var username = $("#phone").val();
            phone_code(this, username);
        });


        /*快捷登陆*/
        $("#quicklogin").click(function () {

            //验证非空
            $.ajax({
                url: "/login/Index/index",
                type: "POST",
                data: ({
                    username: $("#phone").val(),
                    code: $("#phonecodeDiv").val(), //yzm
                    format: $("#quick").val(),
                    source_url_h5: $('#returnUrl').val()
                }),
                dataType: "json",
                beforeSend: function () {
                    var username = $("#phone").val();
                    if (!(myreg.test(username))) {
                        layer.open({
                            content: '请输入手机号!',
                            skin: 'msg',
                            time: 2,
                        })
                        return false;
                    }
                    var captcha = $("#captcha").val();
                    if (captcha == "") {
                        layer.open({
                            content: '请输入图形验证码!',
                            skin: 'msg',
                            time: 2,
                        })
                        return false;
                    }
                    var code = $("#phonecodeDiv").val();
                    if (code == "") {
                        layer.open({
                            content: '请输入6位动态码!',
                            skin: 'msg',
                            time: 2,
                        })
                        return false;
                    }

                },
                success: function (data) {
                    console.log(data)
                    if (data.success == 1) {
                        $("#quicklogin").html("登录中...");
                        setTimeout(function () {
                            location.href = 'https://mk.yanxian.org/' + $('#returnUrl').val();
                        }, 1000);
                    } else {
                        layer.open({
                            content: data.msg,
                            skin: 'msg',
                            time: 2,
                        })

                    }
                }
            });
        })
        $('.back').click(() => {
            window.history.back();
        })
        $('#reg').click(() => {
            console.log("xx");
            location.href='/users/wkreg.ejs';
        })
    </script>
</body>

</html>